package com.lwx.es;

import java.io.IOException;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Before;
import org.junit.Test;

import com.lwx.es.model.ProductESFieldEnum;
import com.lwx.es.model.ProductEsModel;

public class EsMain {
	private EsManager esManager = new EsManager();
	
	@Before
	public void setUp(){
		try {
			esManager.init();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void testIndex() throws ElasticsearchException, IOException{
		for(int i = 0; i < 1; i++){
			ProductEsModel model = new ProductEsModel();
			String space = i % 2 == 0 ? "" : " ";
			model.setId(String.valueOf(i));
			model.setProductName("prduct" + space + i);
			model.setModel("model" + space + i);
			model.setCountryCode("countryCode" + space + i);
			model.setLanguageCode("languageCode" + space + i);
			esManager.index(model);
		}
	}
	
	@Test
	public void testSearch() {
		BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
		//queryBuilder.must(QueryBuilders.termQuery(ProductESFieldEnum.PRODUCTNAME.getValue(), "prduct"));
		queryBuilder.must(QueryBuilders.multiMatchQuery("prduct10", ProductESFieldEnum.PRODUCTNAME.getValue(), ProductESFieldEnum.MODEL.getValue()));
		//queryBuilder.must(QueryBuilders.termQuery(ProductESFieldEnum.COUNTRYCODE.getValue(), "countryCode"));
		System.out.println("queryBuilder: " + queryBuilder.toString());
		SearchResponse  response = esManager.search(queryBuilder);
		System.out.println(response);
	}
}
